import store from '@/store/index.js'
import storage from '@/utils/storage.js'
import {
	BASE
} from '@/const/base.js'
import api from '@/service/config/api.js'
const permisOpt = () => {
	console.log('store.state.userType', store.state.userType)
	switch (store.state.userType) {
		case 1:
			return [
				['删除记录', 13],
				['取消订单', 2, 5, 6, 7],
				['查看物流单', 10, 11],
				['认购清单', 2, 5, 6, 7, 10, 11, 12],
				['上传支付凭证', 5],
				['确认下单', 2],
				['确认收货', 10],
				['确认安装', 11],
				['评价', 12],
				['售后', 12]
			]
		case 2:
			return [
				['删除记录', 13],
				['确认收款', 6, 9],
				['上传物流单', 7],
				['取消订单', 2, 5, 6, 7],
				['查看物流单', 10, 11],
				['认购清单', 2, 5, 6, 7, 10, 11, 12],
				['上传支付凭证', 5],
				['确认下单', 2],
				['确认收货', 10],
				['确认安装', 11],
				['确认发货', 7],
				['评价', 12],
				['售后', 12]
			]
		case 3:
			return store.state.appType == BASE.USER_TYPE ? [
				['删除记录', 13],
				['确认收款', 6, 9],
				['上传物流单', 7],
				['取消订单', 2, 5, 6, 7],
				['查看物流单', 10, 11],
				['认购清单', 2, 5, 6, 7, 10, 11, 12],
				['上传支付凭证', 5],
				['确认下单', 2],
				['确认收货', 10],
				['确认安装', 11],
				['确认发货', 7],
				['评价', 12],
				['售后', 12]
			] : [
				['删除记录', 12, 13],
				['取消订单', 1, 2, 5, 6, 7],
				['查看物流单', 8, 9, 10, 11],
				['认购清单', 1, 2, 5, 6, 7, 8, 9, 10, 11, 12],
				['上传支付凭证', 5],
				['上传尾款凭证', 8],
				['确认下单', 1],
				['确认收货', 10],
				['确认安装', 11],
				['评价', 12],
				['售后', 12]
			]
		case 4:
			return [
				['删除记录', 12, 13],
				['取消订单', 2, 5, 6, 7],
				['查看物流单', 10, 11],
				['认购清单', 2, 5, 6, 7, 10, 11, 12],
				['上传支付凭证', 5],
				['确认收货', 10],
				['确认安装', 11],
				['评价', 12],
				['售后', 12]
			]
		case 5:
			return [
				['删除记录', 13],
				['取消订单', 1, 2, 5, 6, 7],
				['查看物流单', 8, 9, 10, 11],
				['认购清单', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
				['上传支付凭证', 5],
				['上传尾款凭证', 8],
				['确认下单', 2],
				['确认收货', 10],
				['确认安装', 11],
			]
		case 6:
			return [
				['删除记录', 13],
				['取消订单', 2],
				['查看物流单', 10, 11],
				['认购清单', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
				['上传支付凭证', 5],
				['上传尾款凭证', 8],
				['确认下单', 2],
				['立即审核', 3],
			]
		case 7:
			return [
				['删除记录', 13],
				['取消订单', 2],
				['认购清单', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
				['上传支付凭证', 5],
				['上传尾款凭证', 8],
				['确认下单', 2],
				['立即审核', 3, 4],
			]
		case 8:
			return [
				['确认收款', 6, 9],
			]
		case 9:
			return [
				['上传物流单', 7],
				['查看物流单', 8, 9, 10, 11],
			]
	}
}
const permisListOpt = () => {
	switch (store.state.userType) {
		case 1:
			return [
				['上传支付凭证', 5],
				['确认下单', 2],
				['确认收货', 10],
				['确认安装', 11],
			]
		case 2:
			return [
				['确认收款', 6, 9],
				['上传物流单', 7],
				['上传支付凭证', 5],
				['确认下单', 2],
				['确认收货', 10],
				['确认安装', 11],
				['确认发货', 7],
			]
		case 3:
			return store.state.appType == BASE.USER_TYPE ? [
				['确认收款', 6, 9],
				['上传物流单', 7],
				['上传支付凭证', 5],
				['确认下单', 2],
				['确认收货', 10],
				['确认安装', 11],
				['确认发货', 7],
			] : [
				['上传支付凭证', 5],
				['上传尾款凭证', 8],
				['确认下单', 1],
				['确认收货', 10],
				['确认安装', 11],
			]
		case 4:
			return [
				['上传支付凭证', 5],
				['确认收货', 10],
				['确认安装', 11],
			]
		case 5:
			return [
				['上传支付凭证', 5],
				['上传尾款凭证', 8],
				['确认下单', 2],
				['确认收货', 10],
				['确认安装', 11],
			]
		case 6:
			return [
				['上传支付凭证', 5],
				['上传尾款凭证', 8],
				['确认下单', 2],
				['立即审核', 3],
			]
		case 7:
			return [
				['上传支付凭证', 5],
				['上传尾款凭证', 8],
				['确认下单', 2],
				['立即审核', 3, 4],
			]
		case 8:
			return [
				['确认收款', 6, 9],
			]
		case 9:
			return [
				['上传物流单', 7],
				['查看物流单', 8, 9, 10, 11],
			]

	}
}

const permisAuthOpt = ['/base/api/tAppletProtocol/type','/base/api/tAppletProtocol/inner/type','/user/api/auth/getSocialTenantId',
	'/goods/api/product/getBrandList',
	'/goods/api/product/getStyleList', '/goods/api/product/getMaterialList',
	'/goods/api/product/getGoodTypeList', '/goods/api/product/productList',
	'/base/api/tAppletBanner/inner/queryByShowLocation', '/base/api/tAppletBanner/queryByShowLocation',
	'/goods/api/product/getTypeList',
	'/goods/api/product/getFirstPageList', '/base/api/tAppletBanner/inner/queryByShowLocation',
	'/goods/api/product/goodProductList', '/user/api/auth/userInfo', '/user/api/dealer/info',
	'/marketing/applet/api/user/discount_coupon/list/unclaimed', '/user/api/customerSet/addressAndPhone',
	'/marketing/applet/api/user/red_packet/currentActivity', '/goods/api/ShoppingCart/findShoppingCartProducts',
	'/order/api/order/data', '/user/tMyNotice/total', '/base/tDealerBase/item/get',
	'/base/api/dealerComboPay/findDealerCombos', '/user/api/dealer/submitAuth',
	'/base/api/dealerComboPay/placeOrder',
	'/user/api/auth/bindingWechat','/user/api/auth/modifyPassword','/user/api/auth/sendLoginSms','/user/api/auth/getWechatPhone','/user/api/auth/phoneRegister'
]
// 1.导购员 2.店长 3.经销商 4.普通用户 5.业务员 6.业务主管 7.业务总监 8.财务 9.库管
export const permis = (status, operateName, extraOperate) => {
	const options = permisOpt()
	if (!options || status === undefined) return false;
	const item = JSON.parse(JSON.stringify(options)).find(item => item[0] === operateName)
	if (item) {
		const statusList = item.slice(1, item.length)
		if (extraOperate !== undefined) {
			return statusList.includes(status) && !!extraOperate
		}
		return statusList.includes(status)
	}
	return false
}
// 1.导购员 2.店长 3.经销商 4.普通用户 5.业务员 6.业务主管 7.业务总监 8.财务 9.库管
export const permisList = (status, operateName, extraOperate) => {
	const options = permisListOpt()
	if (!options || status === undefined) return false;
	const item = JSON.parse(JSON.stringify(options)).find(item => item[0] === operateName)
	if (item) {
		const statusList = item.slice(1, item.length)
		if (extraOperate !== undefined) {
			return statusList.includes(status) && !!extraOperate
		}
		return statusList.includes(status)
	}
	return false
}

export const permisAuth = (api, noCheck = false) => {
	if (!storage.getItem('token') || permisAuthOpt.some(item=>(new RegExp(`${item.replace(/\//g, "\\/")}(\/.)*`).test(api)))||noCheck) {
		return Promise.resolve()
	}
	console.log('不通过----', api)
	if (!storage.getItem('basicUnserInfo') || !store.state.userType) {
		return new Promise((resolve, reject) => {
			api.get('/user/api/auth/userInfo').then(res => {
				const data = res.data
				storage.setItem('basicUnserInfo', data)
				store.commit('setUserInfo', data)
				let userType = data.identityType
				if (data.identityType == 1) {
					 if (data.dealer) {
						storage.setItem("userType", 3);
						userType = 3
					} else if (!data.manager && !data.guide && !data.dealer) {
						storage.setItem("userType", 4);
						userType = 4
					} else if (data.manager) {
						storage.setItem("userType", 2);
						userType = 2

					} else if (data.guide) {
						storage.setItem("userType", 1);
						userType = 1

					}

				} else if (data.identityType == 2) {
					storage.setItem("userType", 3);
					userType = 3
				} else {
					storage.setItem("userType", data.identityType + 2);
					userType = data.identityType + 2
				}
				store.commit('setUserType', userType)
				return noPermisUrl(api, data, userType, resolve, reject)
			}).catch(() => {
				uni.reLaunch({
					url: '/pages/account/pages/login'
				})
			})
		})
	} else {
		return new Promise((resolve, reject) => {
			return noPermisUrl(api, storage.getItem('basicUnserInfo'), store.state.userType, resolve,
				reject)
		})
	}
}

export const noPermisUrl = (api, basicUnserInfo, userType, resolve, reject) => {
	if (store.state.appType == BASE.USER_TYPE) {
		return resolve()
	}
	if (userType != 3 || (userType == 3 && basicUnserInfo.dealerAuditStatus == 2 && basicUnserInfo
			.paySetMeal)) {
		return resolve()
	}
	uni.reLaunch({
		url: '/pages/common/pages/no_permission'
	})
	return reject()
}